package com.yysoft.dubbo.provider.controller;

import javax.jms.Message;
import javax.jms.TextMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yysoft.dubbo.api.IDemoService;

@Controller
public class ProviderController {

	@Autowired
	private IDemoService demoService;
	
	@Autowired
	private JmsTemplate queueJmsTemplate;
	
	@Autowired
	private JmsTemplate topicJmsTemplate;
	
	@RequestMapping("/test")
	@ResponseBody
	public String test() throws Exception {
		String string = demoService.sayHi("你好！");
		System.out.println("test调用成功:" + string);
		return string;
	}
	
	@RequestMapping("/sendQueue")
	@ResponseBody
	public void sendQueue() throws Exception {
		queueJmsTemplate.send(session -> {
			return session.createTextMessage("你好！");
		});
		System.out.println("sendQueue调用成功");
	}
	
	@RequestMapping("/sendTopic")
	@ResponseBody
	public void sendTopic() throws Exception {
		topicJmsTemplate.send(session -> {
			return session.createTextMessage("你好！");
		});
		System.out.println("sendTopic调用成功");
	}
	
	/**
	 * 该方法只能调用一次
	 * @throws Exception
	 */
	@RequestMapping("/sendAndReceiveQueue")
	@ResponseBody
	public void sendAndReceiveQueue() throws Exception {
		Message message = queueJmsTemplate.sendAndReceive(session -> {
			return session.createTextMessage("你好2！");
		});
		System.out.println("sendAndReceiveQueue调用成功:" + ((TextMessage) message).getText());
	}
	
}
